package com.dps.omc.utils.jdbc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Service
public class CSOracle extends JdbcService{
    private static final Logger logger = LoggerFactory.getLogger(CSOracle.class);
	
	private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static Connection  conn = null;
    static
    {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            logger.error(e.toString());
        }
    }

    public boolean testConnect(JdbcConnect jdbcConnect) {

//        try {
//            Class.forName(DRIVER);
//        } catch (ClassNotFoundException e) {
//            logger.error(e.toString());
//        }

        String URL = "jdbc:"+jdbcConnect.getDbType()+":thin:@"+jdbcConnect.getDbHost()+":"+jdbcConnect.getDbPort()+"/"+jdbcConnect.getDbName();
        String USERNAME = jdbcConnect.getDbUser();
        String PWD = jdbcConnect.getDbPass();

        try {
            conn = DriverManager.getConnection(URL,USERNAME,PWD);
            return true;
        } catch (SQLException e) {
            logger.error(e.toString());
        }
        finally {
            try {
                if(conn!=null)
                {
                    conn.close();
                }
            } catch (SQLException e) {
                logger.error(e.toString());
            }
        }
        return false;
  }
//    public static void main(String[] args){
//
//        String dbName = "oracle";
//        String host = "localhost";
//        String globalname = "orcl";
//        String port = "1521";
//        String username = "scott";
//        String password = "tiger";
//
//        JdbcService jdbc = new CSOracle(dbName, host, globalname, port, username, password);
//        System.out.println(jdbc.testConnect());
//
//    }

}